package com.meixiang.adapter.shopping;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.meixiang.R;
import com.meixiang.entity.shopping.ImageTextEntity;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品图文列表适配器
 *
 * @author LZRUI
 */
public class GoodsImageTextAdapter extends BaseAdapter {

    private List<ImageTextEntity> imageList;
    private Context mContext;

    public GoodsImageTextAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<ImageTextEntity> list) {
        if (list != null) {
            imageList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return imageList == null ? 0 : imageList.size();
    }

    @Override
    public Object getItem(int position) {
        return imageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_webview, parent, false);
            viewHolder = new ViewHolder(convertView);
            Log.e("TAG","返回图片=="+imageList.get(position).url+"--高度=="+imageList.get(position).height+"-----"+imageList.get(position).width+"----屏幕宽高"+ Tool.getWindowWith(mContext)+"---屏幕高度"+Tool.getWindowHeight(mContext));
            Bitmap mBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image_default_white_bg);
            Resources resources = mContext.getResources();
            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();

            //得到当前屏幕宽度
            float windWith=Float.valueOf(Tool.getWindowWith(mContext));
            float windHeight=Float.valueOf(Tool.getWindowHeight(mContext));

            //得到返回的宽高
            float mDataWith=Float.valueOf((imageList.get(position).width));
            float mDataHeight=Float.valueOf((imageList.get(position).height));

                int newWidth = Tool.getWindowWith(mContext);
                int newHeight = Integer.valueOf(imageList.get(position).height);
                // 计算缩放比例
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // 取得想要缩放的matrix参数
                Matrix matrix = new Matrix();
                matrix.postScale(scaleWidth, scaleHeight);
                mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix,
                        true);
                Drawable drawable =new BitmapDrawable(mContext.getResources(),mBitmap);

                /**等比例缩放*/
                float bili=mDataWith/windWith;

                float  ratio=Integer.valueOf(imageList.get(position).width)/Tool.getWindowWith(mContext);
                GlideHelper.showDefaultImage(mContext,imageList.get(position).url,Tool.getWindowWith(mContext),(int)(Integer.valueOf(imageList.get(position).height)/bili),drawable,viewHolder.webView);

            }


        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.webView)
        ImageView webView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
